package com.hxy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxy.common.DateUtil;
import com.hxy.common.JsonResult;
import com.hxy.dao.PaydetailMapper;
import com.hxy.dao.PayrecordMapper;
import com.hxy.dao.StuffinfoMapper;
import com.hxy.pojo.Paydetail;
import com.hxy.pojo.Payrecord;
import com.hxy.pojo.Stuffinfo;
import com.hxy.pojo.model.PayRecordSelectData;
import com.hxy.pojo.vo.PaydetailVo;
import com.hxy.pojo.vo.PayrecordVo;
import com.hxy.pojo.vo.StuffinfoVo;
import com.hxy.service.PayrecordService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class PayrecordServiceImpl implements PayrecordService {
    @Autowired
    private PayrecordMapper payrecordMapper;
    @Autowired
    private PaydetailMapper paydetailMapper;
    @Autowired
    private StuffinfoMapper stuffinfoMapper;
    @Override
    public int deletePayrecordByPrimaryKey(Integer id) {
        return payrecordMapper.deleteByPrimaryKey(id);
    }
    @Override
    public int deletePaydetailByPrimaryKey(Integer id) {
        return paydetailMapper.deleteByPrimaryKey(id.longValue());
    }
    @Override
    public int insert(PayrecordVo Payrecord) {
        return payrecordMapper.insert(Payrecord);
    }
    public  int insert2(PaydetailVo Paydetail){
        return payrecordMapper.insert2(Paydetail);

    }

    public JsonResult update (PayrecordVo payrecord){
        //payrecord表
        String Givename = (String) SecurityUtils.getSubject().getPrincipal();
        payrecord.setGivename(Givename);
        StuffinfoVo stuffinfo = stuffinfoMapper.selectByPrimaryKey(payrecord.getEid());
        payrecord.setPickname(stuffinfo.getStuffname());
        int update = payrecordMapper.update(payrecord);
        if (update>0){
            return new JsonResult(0, "success",update);
        }
        else {
            return new JsonResult(1, "fail",update);
        }

    }



    public int update1 (PaydetailVo Paydetail){
        return payrecordMapper.update1(Paydetail);
    }

    /**
     * 查询已经发放的工资
     * @param payRecordSelectData
     * @return
     */
    @Override
    public PageInfo<Payrecord> selectAlreadyPageInfoAll(PayRecordSelectData payRecordSelectData) {
        PageHelper.startPage(payRecordSelectData.getPage(), payRecordSelectData.getLimit());
        List<Payrecord> payrecords = payrecordMapper.selectAlreadyPageInfoAll(payRecordSelectData);
        PageInfo<Payrecord> payrecordPageInfo = new PageInfo<>(payrecords);
        return payrecordPageInfo;
    }

    @Override
    public PageInfo<Payrecord> selectNotPageInfoAll(PayRecordSelectData payRecordSelectData) {
        PageHelper.startPage(payRecordSelectData.getPage(), payRecordSelectData.getLimit());
        List<Payrecord> payrecords = payrecordMapper.selectNotPageInfoAll(payRecordSelectData);
        PageInfo<Payrecord> payrecordPageInfo = new PageInfo<>(payrecords);
        return payrecordPageInfo;
    }

    @Override
    public JsonResult selectOneByPayRecord(Integer id) {
        PayrecordVo payrecordVo= payrecordMapper.selectOneByPayRecord(id);
        if(payrecordVo.getId()!=null&&!payrecordVo.getId().equals("")){
            return new JsonResult(0,"success",payrecordVo);
        }
        else {
            return new JsonResult(1, "fail",null);
        }
    }

    @Override
    public JsonResult addAlready(String stuffName,String stuffPhone, String sendTime, String receiver, BigDecimal money, String sender, String status) {
        PayrecordVo payrecordVo = new PayrecordVo();
        payrecordVo.setEname(stuffName);
        payrecordVo.setSphone(stuffPhone);
        payrecordVo.setPaydate(sendTime);
        payrecordVo.setPickname(receiver);
        payrecordVo.setPaynumber(money);
        payrecordVo.setGivename(sender);
        payrecordVo.setStatus(Integer.valueOf(status));

        Integer Eid = stuffinfoMapper.selectEidByStuffName(stuffName, stuffPhone);
        payrecordVo.setEid(Eid);
        int id=payrecordMapper.addPayRecordAlready(payrecordVo);
        System.out.println(payrecordVo.getId()+"/////////////////自增主键//////////////////////");
        //生成一个没有工资详情的 paydetail表 有eid /create_time /paystatus
          PaydetailVo paydetailVo=new PaydetailVo();
          paydetailVo.setId(payrecordVo.getId().longValue());
          paydetailVo.setEid(Eid);
          paydetailVo.setCreateTime(DateUtil.date2Str(new Date(),DateUtil.DATE_FORMAT_5));
          paydetailVo.setPaystatus(status);
          paydetailMapper.insertByAddAlready(paydetailVo);
        if (id>0){
             return new JsonResult(0,"success",id);
         }
         else {
             return new JsonResult(1,"fail",id);
         }

    }
}
